// pages/operat/codeRelation/manyRelation/relationProduct/relationProduct.js
const LApp = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    proBatchModel: {},
    productModel: {},
    codetext: [],
    codeAllotModel: {},
    chooceType:'pici',
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.GetProcBatchItemList();
    this.getProductList();
    
    let text = LApp.pages.get("codeRelation/manyRelation/manyRelation").data.text;
    if (text) {
      this.setData({
        codetext: text,
        
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //单选事件函数
  radioChange:function(e){
    this.setData({chooceType: e.detail.value});
  }
  ,
  //获取产品批次下拉数据
  GetProcBatchItemList: function () {
    let _this = this;//保存当前页面数据
    LApp._get({
      url: 'nbapi/productbatch/selectitem',
      data: { enterpriseId: LApp.globalData.userInfo.EnterpriseId },
      success: function (r) {
        _this.setData({
          proBatchList: r
        })
      }
    })
  },
  //bind product's bactch
  proBatchSelect: function (e) {
    var _proBatch = this.data.proBatchList[e.detail.value];
    if (_proBatch) {
      this.data.proBatchModel.BatchName = _proBatch.Name;
      this.data.proBatchModel.BatchNametId = _proBatch.Code;
      this.setData(this.data);
    }
  },
  //get list of product
  getProductList: function () {
    let _this = this;
    LApp._get({
      url: 'nbapi/product/selectitem',
      data: { enterpriseId: LApp.globalData.userInfo.EnterpriseId },
      success: function (r) {
        _this.setData({
          proudctList: r
        })
      }
    })
  },
  //bind product 
  productSelect: function (e) {
    var _product = this.data.proudctList[e.detail.value];
    if (_product) {
      this.data.productModel.Product = _product.Name;
      this.data.productModel.ProductId = _product.Code;
      this.setData(this.data);
    }
  },
  // when u click save
  saveProRelation: function (e) {
    if (this.data.chooceType == 'pici') {//if select product's batch
      this.data.productModel.ProductId=null;
    } else {//if select product
      this.data.proBatchModel.BatchNametId=null;
    }
    var proId = this.data.productModel.ProductId;
    var batId = this.data.proBatchModel.BatchNametId;
    var userInfo = LApp.globalData.userInfo;
    this.data.codeAllotModel.arrCode = this.data.codetext;
    this.data.codeAllotModel.ApplyProductId = this.data.productModel.ProductId;
    this.data.codeAllotModel.ApplyProduct = this.data.productModel.Product;
    this.data.codeAllotModel.ApplyBatchId = this.data.proBatchModel.BatchNametId;
    this.data.codeAllotModel.CreateUserId = userInfo.UserID;
    this.data.codeAllotModel.CreateEnterpriseId = userInfo.EnterpriseId;
    this.data.codeAllotModel.AllotToCoId = userInfo.EnterpriseId;
    this.data.codeAllotModel.AllotBatch = "无";
    this.data.codeAllotModel.AllotToCo = userInfo.EnterpriseName;
    if (proId && batId) {
      wx.showModal({
        title: '提示',
        content: '产品批次和产品名称有且仅能选一样',
        showCancel: false
      })
      return;
    }

    if (proId) {//if select product
      this.data.codeAllotModel.type = 1;
      this.data.codeAllotModel.ApplyBatchId = 0;
    }
    else if (batId) {//if select product's batch
      this.data.codeAllotModel.type = 2;
      this.data.codeAllotModel.ApplyProductId = 0;
      this.data.codeAllotModel.ApplyProduct = "无";
    }
    else //select nothing
    {
      wx.showModal({
        title: '提示',
        content: '请选择产品或批次',
        showCancel: false
      })
      return;
    }

    LApp._post({
      url: 'nbapi/codemanager/code/relation',
      data: this.data.codeAllotModel,
      success: function (r) {
        if (r == true) {
          wx.showToast({
            title: '关联成功',
            icon: "success",
            success:function(){
              //返回上一个页面
             wx.navigateBack({
               delta:1
             })
            }
          })
          
        }
        else {
          wx.showModal({
            title: '提示',
            content: r,
            // showCancel: false
            
          })
        }

      }
    })
  }
})